<html>
<head>
  <title>How To Use Timer_B...</title>
</head>

<body>

<!-- BEGIN LAB DESCRIPTION -->

<font face="Arial">
<!------------------------------------------------------------------------------------>
<a name="HowToTimerB">
<h2>How To Use Timer_B for PWM of Tones...</h2>
</a>

<p>Pulse-width modulation, or PWM, provides an adequate substitute for a Digital-to-Analog
(DAC) in many applications.  By varying the width of pulses sent to a device (such
as a speaker or LED), a purely digital signal can fabricate an acceptable analog wave form.

<p>The periodic timer Timer_B of the MSP430 can be configured in four different modes:
stop, up, continuous, or up/down mode.  At the end of a period (whether by reaching
a maximum count or a compared value), Timer_B can be used
to generate an interrupt on the Timer_B interrupt vector.

<p>The period of Timer_B, when configured to use <b>SMCLK</b> (<b>TBSSEL_2</b>) and set to "up" mode
(<b>MC_1</b>), allows periodic outputs
to be produced completely automatic by hardware, without any software intervention.
Thus, the MSP430 can be left undisturbed in low-power mode (LPM0) while hardware
continues to produce outputs (ie, interrupts or other port outputs).

<p>In up mode, Timer_B register <b>TBR</b> counts from 0 up to the value in
register <b>TBCCR0</b>.  <b>TBR</b> is reset to 0 on the next clock transition.
(Thus the period is <b>TBCCR0</b>+1 counts.)  This makes the outputs of Timer_B at
the same frequency, which is useful for pulse-width modulation.

<p>To output a PWM signal on <b>TB2</b> (P4.5), configure Timer_B to use
   <b>SMCLK</b> as the clock source, use appropriate clock signal divider
   (/1, /2, /4, or /8), set output mode "set/reset", and put the
   timer in "up" mode,

<blockquote><pre>    clr.w  &TBR                       ; configure Timer_B
    mov.w  #TBSSEL_2+ID_0+MC_1,&TBCTL ; SMCLK, /1, up mode
    mov.w  #OUTMOD_3,&TBCCTL2         ; output mode = set/reset
    bis.b  #0x20,&P4SEL               ; select alternate output (TB2) for P4.5</pre></blockquote>

<p>To set the tone pitch (frequency) and volume (duty cycle) for
   Timer_B,

<blockquote><pre>    mov.w  r12,&TBCCR0                ; set Timer_B period (frequency)
    rra.w  r12
    mov.w  r12,&TBCCR2                ; set 50% duty cycle</pre></blockquote>

<p>To turn off Timer_B,

<blockquote><pre>    mov.w  #0,&TBCCR0                 ; set Timer_B mode = stop</pre></blockquote>


<p>See MMB pgs. 287-289, 296-300, 330-349 and MSP430x2xx Family User's Guide.pdf, Chapter 12. 
<!-- END LAB DESCRIPTION -->

</body>
</html>
